Testing Backbone.js by Roemer Ryan

Testing Backbone.js by Roemer Ryan

Author:Roemer, Ryan
Language: eng
Format: epub
Publisher: Packt Publishing


Anonymous spies

Spies can be created as anonymous standalone functions, which are often used to test event logic in Backbone.js applications. For example, we create a Backbone.js event object and an anonymous Sinon.JS spy in the following code. The spy listens to the foo event, which we trigger. Then, we can inspect the spy and assert that the spy was called once and passed 42 as a parameter:

it("calls anonymous spy on event", function () { var eventer = _.extend({}, Backbone.Events), spy = sinon.spy(); // Set up the spy. eventer.on("foo", spy); expect(spy.called).to.be.false; // Fire event. eventer.trigger("foo", 42); // Check number of calls. expect(spy.calledOnce).to.be.true; expect(spy.callCount).to.equal(1); // Check calling arguments. expect(spy.firstCall.args[0]).to.equal(42); expect(spy.calledWith(42)).to.be.true; });



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.